package org.hot100_2;

public class leetcode155 {

    class MinStack {

        class Node{
            int value;
            int min;
            Node next;


            //在创建dummy，需要指定dummy的min
            Node(){
                this.min=Integer.MAX_VALUE;
            }
            Node(int val){
                this.min=Integer.MAX_VALUE;
                this.value=val;
            }
        }


        Node dummy = new Node();

        public MinStack() {
        }

        public void push(int val) {
            Node newNode = new Node(val);

            newNode.next=dummy.next;
            newNode.min= dummy.min;

            dummy.next=newNode;
            dummy.min=Math.min(val,dummy.min);
        }

        public void pop() {
            Node popNode = dummy.next;
            dummy.next=popNode.next;
            popNode.next=null;
            if (dummy.min==popNode.value){
                dummy.min= popNode.min;
            }
        }

        public int top() {
            return dummy.next.value;
        }

        public int getMin() {
            return dummy.min;
        }
    }
}
